#include "gamearea.h"

GameArea::GameArea()
{
    field = new SnakeField;
}

QRectF GameArea::boundingRect() const
{
    qreal x0,y0,x,y;
    y = field->getFieldSize().first*CELL_SIZE;
    y0 = - y/2.0;
    x = field->getFieldSize().second*CELL_SIZE;
    x0 = - x/2.0;
    return QRectF(x0,y0,x,y);
}

void GameArea::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

    Field map = field->getField();
    qreal x0,y0;
    x0 = -(field->getFieldSize().first*(qreal)CELL_SIZE)/2.0;
    y0 = -(field->getFieldSize().second*(qreal)CELL_SIZE)/2.0;

    Field::iterator it = map.begin();
    quint32 i =0;
    while(it != map.end())
    {
        QVector<Cell >::iterator jt = it->begin();
        quint32 j = 0;
        while(jt != it->end())
        {
            QRectF rec(x0+j*CELL_SIZE,y0+i*CELL_SIZE,CELL_SIZE-1,CELL_SIZE-1);
            QBrush brush;
            if(*jt == CELL_FREE)
                brush = QBrush(QColor(CELL_FREE_BRUSH));
            else
                brush = QBrush(QColor(CELL_NOT_FREE_BRUSH));
            painter->fillRect(rec,brush);
            j++;
            jt++;
        }
        i++;
        it++;
    }

    foreach(quint32 curID, bonuses.keys())
    {
        Point curPoint(bonuses.value(curID)->getBonusPos());
        QRectF rec(x0+curPoint.second*CELL_SIZE,y0+curPoint.first*CELL_SIZE,CELL_SIZE-1,CELL_SIZE-1);
        QBrush brush;
        if(bonuses.value(curID)->getBonusType() == BONUS_SIMPLE)
            brush = QBrush(QColor(BONUS_SIMPLE_BRUSH));
        painter->fillRect(rec,brush);
    }

    foreach(quint32 curID, snakes.keys())
    {
        QList<Point > curBody = snakes.value(curID)->getBody();
        foreach(Point curPoint, curBody)
        {
            QRectF rec(x0+curPoint.second*CELL_SIZE,y0+curPoint.first*CELL_SIZE,CELL_SIZE-1,CELL_SIZE-1);
            QBrush brush;
            if(curID == mySnakeID)
                brush = QBrush(QColor(CELL_MY_SNAKE_BRUSH));
            else
                brush = QBrush(QColor(CELL_OTHER_SNAKE_BRUSH));
            painter->fillRect(rec,brush);
        }
    }
}

void GameArea::setFieldSize(Point size)
{
    field->setFieldSize(size);
    prepareGeometryChange();
}

void GameArea::setField(QString map)
{
    field->setField(map);
}

void GameArea::setMySnakeID(quint32 ID)
{
    mySnakeID = ID;
}

void GameArea::addSnake(quint32 ID, QList<Point> body)
{
    SnakeSnake *snake = new SnakeSnake;
    snake->createSnake(body);
    snakes.insert(ID,snake);
    playersPoints.insert(ID,0);
}

void GameArea::deleteSnake(quint32 ID)
{
    snakes.remove(ID);
    playersPoints.remove(ID);
}

void GameArea::addBonus(quint32 ID, Point pos, quint32 type)
{
    SnakeBonus *bonus = new SnakeBonus;
    bonus->setBonus(pos, type);

    bonuses.insert(ID, bonus);
}

void GameArea::replaceBonus(quint32 ID, Point pos, quint32 type)
{
    bonuses.value(ID)->setBonus(pos,type);
}

void GameArea::deleteBonus(quint32 ID)
{
    bonuses.remove(ID);
}

void GameArea::increaseSnake(quint32 ID)
{
    snakes.value(ID)->increase();
}

void GameArea::decreaseSnake(quint32 ID)
{
    snakes.value(ID)->decrease();
}



void GameArea::addPointToPlayer(quint32 ID, quint32 points)
{
    playersPoints[ID] += points;
}

QList<QPair<quint32, quint32> > GameArea::getPointOfPlayers()
{
    QList<QPair<quint32, quint32> > ret;
    foreach(quint32 curID, playersPoints.keys())
    {
        ret.push_back(QPair<quint32, quint32>(curID, playersPoints.value(curID)));
    }
    return ret;
}



void GameArea::killSnakeAndRespawn(quint32 ID, QList<Point> body)
{
    snakes.value(ID)->killSnake();
    snakes.value(ID)->createSnake(body);
}


void GameArea::moveSnake(quint32 ID, char direction)
{
    Point head = snakes.value(ID)->getHead();
    switch(direction)
    {
    case 'u':
        if(head.first == 0)
            head.first = field->getFieldSize().first - 1;
        else
            head.first--;
        break;
    case 'd':
        if(head.first == (field->getFieldSize().first -1) )
            head.first = 0;
        else
            head.first++;
        break;
    case 'r':
        if(head.second == (field->getFieldSize().second -1))
            head.second = 0;
        else
            head.second++;
        break;
    case 'l':
        if(head.second == 0)
            head.second = field->getFieldSize().second - 1;
        else
            head.second--;
        break;
    }
    snakes.value(ID)->move(head);
}






























